package com.offcn.scwuser.controller;

import com.offcn.dycommon.response.AppResponse;
import com.offcn.scwuser.compoent.SmsTemplate;
import com.offcn.scwuser.pojo.TMember;
import com.offcn.scwuser.service.UserService;
import com.offcn.scwuser.vo.req.UserRegistVo;
import com.offcn.scwuser.vo.resp.UserRespVo;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Api(tags = "用户的模块--包含登录")
public class UserLoginController {
    @Autowired
    private SmsTemplate smsTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserService userService;
    @ApiOperation("获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNum",value ="手机号码" ,required = true)
    })
    @GetMapping("/sendCode")
    public AppResponse<Object> sendCode(String phoneNum){
        //1.生成验证码
        String code = UUID.randomUUID().toString().replace("-", "").substring(0, 6);
        //保存redis
        redisTemplate.opsForValue().set(phoneNum,code,60*60*24*7, TimeUnit.SECONDS);

        //2.发送验证码
        Map<String,String> query = new HashMap();
        query.put("mobile",phoneNum);
        query.put("param", "code:"+code);
        query.put("tpl_id", "TP1711063");
        String sendCode = smsTemplate.sendCode(query);
        //3.响应结果
        if(sendCode.equals("") || sendCode.equals("fail")){
            return AppResponse.fail("发送短信失败");
        }else {
            return AppResponse.ok(sendCode);
        }
    }

    //注册
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public AppResponse<Object> register(UserRegistVo registVo){
        TMember tMember = new TMember();

        //1.核验验证码
        String s = redisTemplate.opsForValue().get(registVo.getLoginacct());
        if (s != null && s.equals(registVo.getCode())){
            BeanUtils.copyProperties(registVo,tMember);
            try {
                userService.registerUser(tMember);
                redisTemplate.delete(registVo.getLoginacct());
                return AppResponse.ok("注册成功");
            } catch (Exception e) {
                e.printStackTrace();
                return AppResponse.fail(e.getMessage());
            }
        }else{
            return AppResponse.ok("验证码错误");
        }


    }

    //登录
    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true)
    })//@ApiImplicitParams：描述所有参数；@ApiImplicitParam描述某个参数
    @PostMapping("/login")
    public AppResponse<UserRespVo> login(String username, String password) {
        //1、尝试登录
        TMember member = userService.login(username, password);
        if (member == null) {
            //登录失败
            AppResponse<UserRespVo> fail = AppResponse.fail(null);
            fail.setMsg("用户名密码错误");
            return fail;
        }else{
        //2、登录成功;生成令牌
            String token = UUID.randomUUID().toString().replace("-", "");
            redisTemplate.opsForValue().set(token,member.getId()+"",7,TimeUnit.DAYS);
            UserRespVo userRespVo = new UserRespVo();
            BeanUtils.copyProperties(member,userRespVo);//完成属性赋值userRespVo.setAccessToken(token);
            userRespVo.setAccessToken(token);
            return AppResponse.ok(userRespVo);
        }


    }
}
